﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
namespace He.DAL
{
	/// <summary>
	/// 数据访问类:ad_position
	/// </summary>
	public partial class ad_position
	{
		public ad_position()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("id", "he_ad_position"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from he_ad_position");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(He.Model.ad_position model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into he_ad_position(");
			strSql.Append("title,display_effect,call_mark,ad_status,ad_width,ad_height,ad_target,allow_set_effect,lang)");
			strSql.Append(" values (");
			strSql.Append("@title,@display_effect,@call_mark,@ad_status,@ad_width,@ad_height,@ad_target,@allow_set_effect,@lang)");
			strSql.Append(";select LAST_INSERT_ROWID()");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@display_effect", DbType.String),
					new SQLiteParameter("@call_mark", DbType.String),
					new SQLiteParameter("@ad_status", DbType.Int32,8),
					new SQLiteParameter("@ad_width", DbType.String),
					new SQLiteParameter("@ad_height", DbType.String),
					new SQLiteParameter("@ad_target", DbType.String),
					new SQLiteParameter("@allow_set_effect", DbType.Int32,8),
					new SQLiteParameter("@lang", DbType.String)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.display_effect;
			parameters[2].Value = model.call_mark;
			parameters[3].Value = model.ad_status;
			parameters[4].Value = model.ad_width;
			parameters[5].Value = model.ad_height;
			parameters[6].Value = model.ad_target;
			parameters[7].Value = model.allow_set_effect;
			parameters[8].Value = model.lang;

			object obj = DbHelperSQLite.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(He.Model.ad_position model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update he_ad_position set ");
			strSql.Append("title=@title,");
			strSql.Append("display_effect=@display_effect,");
			strSql.Append("call_mark=@call_mark,");
			strSql.Append("ad_status=@ad_status,");
			strSql.Append("ad_width=@ad_width,");
			strSql.Append("ad_height=@ad_height,");
			strSql.Append("ad_target=@ad_target,");
			strSql.Append("allow_set_effect=@allow_set_effect,");
			strSql.Append("lang=@lang");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@title", DbType.String),
					new SQLiteParameter("@display_effect", DbType.String),
					new SQLiteParameter("@call_mark", DbType.String),
					new SQLiteParameter("@ad_status", DbType.Int32,8),
					new SQLiteParameter("@ad_width", DbType.String),
					new SQLiteParameter("@ad_height", DbType.String),
					new SQLiteParameter("@ad_target", DbType.String),
					new SQLiteParameter("@allow_set_effect", DbType.Int32,8),
					new SQLiteParameter("@lang", DbType.String),
					new SQLiteParameter("@id", DbType.Int32,8)};
			parameters[0].Value = model.title;
			parameters[1].Value = model.display_effect;
			parameters[2].Value = model.call_mark;
			parameters[3].Value = model.ad_status;
			parameters[4].Value = model.ad_width;
			parameters[5].Value = model.ad_height;
			parameters[6].Value = model.ad_target;
			parameters[7].Value = model.allow_set_effect;
			parameters[8].Value = model.lang;
			parameters[9].Value = model.id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_ad_position ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from he_ad_position ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public He.Model.ad_position GetModel(int id)
		{		
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,display_effect,call_mark,ad_status,ad_width,ad_height,ad_target,allow_set_effect,lang from he_ad_position ");
			strSql.Append(" where id=@id");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
			parameters[0].Value = id;

			He.Model.ad_position model=new He.Model.ad_position();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["title"]!=null && ds.Tables[0].Rows[0]["title"].ToString()!="")
				{
					model.title=ds.Tables[0].Rows[0]["title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["display_effect"]!=null && ds.Tables[0].Rows[0]["display_effect"].ToString()!="")
				{
					model.display_effect=ds.Tables[0].Rows[0]["display_effect"].ToString();
				}
				if(ds.Tables[0].Rows[0]["call_mark"]!=null && ds.Tables[0].Rows[0]["call_mark"].ToString()!="")
				{
					model.call_mark=ds.Tables[0].Rows[0]["call_mark"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ad_status"]!=null && ds.Tables[0].Rows[0]["ad_status"].ToString()!="")
				{
					model.ad_status=int.Parse(ds.Tables[0].Rows[0]["ad_status"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ad_width"]!=null && ds.Tables[0].Rows[0]["ad_width"].ToString()!="")
				{
					model.ad_width=ds.Tables[0].Rows[0]["ad_width"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ad_height"]!=null && ds.Tables[0].Rows[0]["ad_height"].ToString()!="")
				{
					model.ad_height=ds.Tables[0].Rows[0]["ad_height"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ad_target"]!=null && ds.Tables[0].Rows[0]["ad_target"].ToString()!="")
				{
					model.ad_target=ds.Tables[0].Rows[0]["ad_target"].ToString();
				}
				if(ds.Tables[0].Rows[0]["allow_set_effect"]!=null && ds.Tables[0].Rows[0]["allow_set_effect"].ToString()!="")
				{
					model.allow_set_effect=int.Parse(ds.Tables[0].Rows[0]["allow_set_effect"].ToString());
				}
				if(ds.Tables[0].Rows[0]["lang"]!=null && ds.Tables[0].Rows[0]["lang"].ToString()!="")
				{
					model.lang=ds.Tables[0].Rows[0]["lang"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,title,display_effect,call_mark,ad_status,ad_width,ad_height,ad_target,allow_set_effect,lang ");
			strSql.Append(" FROM he_ad_position ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM he_ad_position ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from he_ad_position T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "he_ad_position";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

